﻿<?php
require_once('../../../config.php');
 require_once($CFG->libdir . '/adminlib.php');
 require_once($CFG->dirroot . '/local/myplugin/lib.php');
require_once($CFG->dirroot . "/local/myplugin/forms.php");
//require_once ($CFG->dirroot .'/local/myplugin/PHPExcel/IOFactory.php');
require_once ($CFG->dirroot .'/local/myplugin/PHPExcel.php');

require_login($SITE);

if (!is_siteadmin()) {
    print_error("vui lòng đăng nhập với quyền admin để thao tác");
}

global $DB;
$option=array();

/* Ket noi den file javascript - module.js */
$PAGE->requires->js_init_call('M.local_myplugin');	
$cssdir =  $CFG->wwwroot.'/local/myplugin/css/mycss.css';
echo '<link rel="stylesheet" href="'.$cssdir.'" type="text/css" media="screen" />';
//echo '<link rel="stylesheet" href="style.css" type="text/css" media="screen" />';
echo '<script src="../colorbox/jquery.min.js"></script>';
echo '<link rel="stylesheet" href="../css/colorbox.css" />';
echo '<script src="../colorbox/jquery.min.js"></script>';

echo '<script src="../colorbox/jquery.colorbox.js"></script>';



/* Chuan bi trang web can hien thi */	
$url = new moodle_url($CFG->wwwroot.'local/myplugin/lo/import_subject.php');
$PAGE->set_url($url);
$PAGE->set_title('Import Subject');
$PAGE->set_heading('Import Subject');
$PAGE->set_pagelayout('standard');

//Them thanh menu ngang ung voi trang trong duong link
navigation_node::override_active_url(new moodle_url($CFG->wwwroot.'/local/myplugin/curriculum/import_subject.php', array('param'=>'value')));

echo $OUTPUT->header();

/* Viet doan code cua ban o day */

$mylib = new learning_outcome_management();
//get form data
$add_curr_form = new form_import_subject();
$add_curr_form->display();
echo '<div id="lodiv"></div>';	
//bat su kien nhan nu cancel
if ($add_curr_form->is_cancelled()) {

echo 'hủy thao tác';

}

//kiem tra neu dc submitted thì làm gì
if ($fromform = $add_curr_form->get_data()) 
{
	$zipdir = my_mktempdir($CFG->tempdir.'/', 'TempLO');
    $dstfile = $zipdir.'/lo.xls';
	$program_id=$fromform->cbprogram;
	if (isset($fromform->submitpreview)) 
	{
        if (!$add_curr_form->save_file('userpicturesfile', $dstfile, true)) {
            @remove_dir($zipdir);
        } 
		else
		{
			$cssdir =  $CFG->wwwroot.'/local/myplugin/module.js';
				echo '<script src="'.$cssdir.'"></script>';
				$dstfile = str_replace('\\', '/', $dstfile); 
				echo '<script type="text/javascript">preview_lo("'.$dstfile.'");</script>';
		}
	}
	else if (isset($fromform->submitbutton)) 
	{
		//luu vao thu muc tam
        if (!$add_curr_form->save_file('userpicturesfile', $dstfile, true)) {
		//neu luu bi loi gi thi xoa file moi upload len
            @remove_dir($zipdir);
        } 
		else
		{
			//neu luu thanh cong thi thao tac
			$cssdir =  $CFG->wwwroot.'/local/myplugin/module.js';
				echo '<script src="'.$cssdir.'"></script>';
				$dstfile = str_replace('\\', '/', $dstfile); 
				echo '<script type="text/javascript">import_subject("'.$dstfile.'");</script>';
		}
	}	
	
}
function my_mktempdir($dir, $prefix='') 
	{
		global $CFG;

		if (substr($dir, -1) != '/') {
			$dir .= '/';
		}

		do {
			$path = $dir.$prefix.mt_rand(0, 9999999);
		} while (file_exists($path));

		check_dir_exists($path);

		return $path;
	}
function lastIndexOf($string,$item){  
		$index=strpos(strrev($string),strrev($item));  
		if ($index){  
			$index=strlen($string)-strlen($item)-$index;  
			return $index;  
		}  
			else  
			return -1;  
	}

/*------Ket thuc doan code cua ban-------*/

echo $OUTPUT->footer();

